package com.hjb.toolbardemo.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;

import com.hjb.toolbardemo.R;
import com.hjb.toolbardemo.base.BaseActivity;
import com.hjb.toolbardemo.base.BaseFragment;
import com.hjb.toolbardemo.fragment.OneFragment;


/**
 * 作者 : hjb
 * 时间 : 2016/4/25.
 */
public class ContainerActivity extends BaseActivity {

    public static final String EXTRA_FRAGMENT_CLASS_NAME = "class_name";
    private BaseFragment mFrg;
    private String className;
    private Class<?> fragmentC;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);

        Bundle bundle = getIntent().getExtras();
        className = bundle.getString(EXTRA_FRAGMENT_CLASS_NAME);
        setContentView(R.layout.activity_container);
        try {
            if (className != null) {
                fragmentC = Class.forName(className);
                mFrg = (BaseFragment) fragmentC.newInstance();
                mFrg.setArguments(bundle);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        FragmentManager supportFragmentManager = getSupportFragmentManager();

        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
        transaction.addToBackStack(fragmentC.getSimpleName());
        transaction.replace(R.id.container_fl, mFrg);
        transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
        transaction.commit();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mFrg.onKeyDown(keyCode, event)) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    /**
     * 打开容器activity
     *
     * @param context
     * @param frgClass
     * @param extras
     */
    public static void startActivity(Activity context, Class<? extends BaseFragment> frgClass, Bundle extras) {
        Intent i = new Intent(context, ContainerActivity.class);
        i.putExtra(EXTRA_FRAGMENT_CLASS_NAME, frgClass.getName());
        if (extras != null)
            i.putExtras(extras);

        context.startActivity(i);
        context.overridePendingTransition(0, 0);
    }

    public static void startActivityForResurt(BaseActivity activity, Class<? extends BaseFragment> frgClass, Bundle extras,
                                              int requestCode) {
        Intent i = new Intent(activity, ContainerActivity.class);
        i.putExtra(EXTRA_FRAGMENT_CLASS_NAME, frgClass.getName());

        if (extras != null)
            i.putExtras(extras);
        activity.startActivityForResult(i, requestCode);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            if (requestCode == 13) {
                (new OneFragment()).onActivityResult(requestCode, resultCode, data);
            }
        }
    }
}
